package com.foreknow.dao.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.util.OraclePagination;
import com.foreknow.util.Pagination;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDAO implements GuestbookDao {
    @Override
    public boolean addGuestbook(Guestbook gb) throws SQLException {
        String sql = "insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?)";
        int isRigth = jdbcTemplate.update(sql,gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedTime());
        return isRigth==1;
    }

    @Override
    public List<Object> queryAll() throws SQLException {
        String sql = "select * from guestbook";
       EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
       List<Object> list = jdbcTemplate.query(sql,mapping);
       return list;
    }
    @Override
    public boolean delete(int id){
        String sql = "delete from guestbook where id=?";
        int isRight = jdbcTemplate.update(sql,id);
        return isRight==1;
    }

    @Override
    public Pagination getGuestbookByPage(int pageSize, int pageNumber) {
        String sql = "select * from guestbook";
        Pagination pagination = new OraclePagination(sql,pageSize,pageNumber);
       return pagination;
}
//    @Override
//    public List<Object> getQuerAll() throws SQLException{
//        String sql = "select * from quwstbook";
//        EntityMapping mapping = this.mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
//        List<Object> list = this.jdbcTemplate.query(sql,mapping);
//        return list;
//    }
}
